﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class 出库单编辑 : Form
    {
        public static int ckdh = 0;//出库单号
        public bool is_new = true;//是否为新单的标志
        public int c0_flag = 0;//出库单flag
        public int rkd_count = 1;//出库单对应的入库单数

        public string cmdtext3 = "  BEGIN TRANSACTION  ";
        public DataTable dt = new DataTable();
        public DataTable dt3 = new DataTable();

        public 出库单编辑(int dh, int flag)
        {
            ckdh = dh;
            c0_flag = flag;
            InitializeComponent();
            first_show();
            
        }

        private void button3_Click(object sender, EventArgs e)//关闭
        {
            this.Close();
        }


        public void first_show()
        {
            int flag = 0;
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext6 = "select flag from yk_ckd0 where CKDH = " + ckdh;
                SqlCommand com = new SqlCommand(cmdtext6, cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    flag = (int)read[0];
                }
                read.Close();
                string cmdtext1 = "SELECT distinct C1.ypbm 编码,YP.MC AS 名称  " +
                                " FROM YK_CKD1 C1     " +
                                "LEFT JOIN ZD_YP YP ON C1.YPBM = YP.BM   " +
                                " WHERE C1.CKDH = " + ckdh;
                SqlDataAdapter da = new SqlDataAdapter(cmdtext1, cn);

                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.Columns[1].Width = 200;
                if (dt.Rows.Count > 0)
                    is_new = false;

            }
            if (!is_new)
            {
                if (flag != 16)
                {
                    this.dataGridView1.AllowUserToAddRows = false;
                    textBox1.ReadOnly = true;
                    textBox1.Enabled = false;
                }
            }
            else

                this.dataGridView1.AllowUserToAddRows = true;

        }


        public void selectRow(string ypbm)
        {
            dt3.Rows.Clear();
            dataGridView3.DataSource = dt3;
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext2 = "  SELECT  C1.ypbm 编码, YP.MC AS 名称,YP.GG as 规格,yp.bzdw as 单位,C1.ypkc as 药品库存, " +
                                   " c1.ncsl as 拟出数,sum(C1.SL) as  实出数,sum(C1.cbje) as 进价金额,C1.pjje as  批价金额,sum(C1.je) as  零价金额,C1.bz as  备注    " +
                                   "FROM YK_CKD1 C1  " +
                                   "   LEFT JOIN ZD_YP YP ON C1.YPBM = YP.BM" +
                                    "  WHERE C1.ckdh =" + ckdh + "  and C1.YPBM='" + ypbm + "'" +
                                    "   group by C1.YPBM,yp.mc,yp.gg,yp.bzdw,c1.ypkc,c1.ncsl,C1.pjje,C1.bz  ";
                SqlCommand com = new SqlCommand(cmdtext2, cn);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    textBox1.Text = dr["编码"].ToString();
                    textBox2.Text = dr["名称"].ToString();
                    textBox3.Text = dr["规格"].ToString();
                    textBox4.Text = dr["单位"].ToString();
                    textBox5.Text = dr["药品库存"].ToString();
                    textBox6.Text = dr["实出数"].ToString();
                    textBox7.Text = dr["拟出数"].ToString();
                    textBox8.Text = dr["批价金额"].ToString();
                    textBox9.Text = dr["进价金额"].ToString();
                    textBox10.Text = dr["备注"].ToString();
                    textBox11.Text = dr["零价金额"].ToString();

                }
                else
                {
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                    textBox5.Text = "";
                    textBox6.Text = "";
                    textBox7.Text = "";
                    textBox8.Text = "";
                    textBox9.Text = "";
                    textBox10.Text = "";
                    textBox11.Text = "";
                }
                dr.Close();
            }
            
            if (c0_flag == 19)
            {
                button1.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;

            }
            else if (c0_flag == 17)
            {
                button1.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;


            }
            else if (c0_flag == 16)
            {
                button1.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;

            }

            //datagridview3
            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                string cmdtext8 = "select   rkdh as 入库单号,pzwh as 批准文号,(select  convert(datetime,yxrq-2)) as 有效期,ph as 批号,c1.sl as 出库数量,sysl as 剩余数量,jj as 进价,lsj as 零售价,gys.mc as 供应商,r1.bz as 备注   " +
                                           "    from yk_rkd1  r1   LEFT JOIN YK_ZDGYS gys ON R1.gysbm = gys.BM   LEFT JOIN yk_ckd1 c1 ON c1.autorkid=r1.autorkid    " +
                                           "where r1.autorkid in (select autorkid from yk_ckd1 where ckdh=" + ckdh + " and  ypbm='" + textBox1.Text.ToString() + "' ) and c1.ckdh=" + ckdh+ "  order by r1.autorkid";
                SqlDataAdapter da = new SqlDataAdapter(cmdtext8, cn);
                da.Fill(dt3);
                dataGridView3.DataSource = dt3;
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            update_currentRow();
            selectRow(dataGridView1.CurrentRow.Cells["编码"].Value.ToString());
        }

        private void 出库单编辑_Load(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            dataGridView2.AllowUserToAddRows = false;
            dataGridView2.MultiSelect = false;
            dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView2.ReadOnly = true;

            dataGridView3.AllowUserToAddRows = false;
            dataGridView3.MultiSelect = false;
            dataGridView3.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView3.ReadOnly = true;
            
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.MultiSelect = false;
            if (c0_flag != 0)
            {
                dataGridView1.CurrentRow.Selected = true;
                dataGridView1.ReadOnly = true;
                selectRow(dataGridView1.CurrentRow.Cells["编码"].Value.ToString());
            }
        }

        private void button5_Click(object sender, EventArgs e)//删除
        {
            string ypbm = dataGridView1.CurrentRow.Cells["编码"].Value.ToString();
            cmdtext3 = cmdtext3 + "   DELETE from YK_CKD1 WHERE CKDH = " + ckdh + "  AND YPBM = '" + ypbm + "'  ";
            dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
            dataGridView1.Rows[0].Selected = true;
            selectRow(dataGridView1[0, 0].Value.ToString());
        }

        private void button4_Click(object sender, EventArgs e)//新增
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入药品");
                return;
            }
            cmdtext3 = cmdtext3 + "    insert into yk_ckd1 (ckdh,ypbm,bzsl,autorkid,sl,dj,cbdj)  " +
                                  "    select top  " + rkd_count + "   " + ckdh + ",'" + textBox1.Text.ToString() + "',bzsl,autorkid,sysl,lsj,jj from yk_rkd1 where  ypbm='" + 
                                    textBox1.Text.ToString() + "'  and sysl>0 order by autorkid    ";

            if (rkd_count > 1)
            {
                Decimal sysl1 = 0;
                for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
                    sysl1 = sysl1 + System.Convert.ToDecimal(dataGridView3[4, i].Value);
                cmdtext3 = cmdtext3 + "    update yk_ckd1 set sl=" + (System.Convert.ToDecimal(textBox6.Text) - sysl1) + "  where  autockid in (select top 1 autockid from yk_ckd1 where  ckdh=" +
                                  ckdh + "   and  ypbm='" + textBox1.Text.ToString() + "' order by autorkid desc)    ";
            }
            else
                cmdtext3 = cmdtext3 + "    update yk_ckd1  set sl=" + textBox6.Text.ToString() +
                               "     where ckdh=" +
                                ckdh + "   and ypbm='" + textBox1.Text.ToString() + "'    ";

            cmdtext3 = cmdtext3 + "  update   yk_ckd1 set dj=yp.bzdj  from yk_ckd1 left join zd_yp yp on ypbm=yp.bm    where ckdh=" + ckdh + "   and ypbm='" + textBox1.Text.ToString() + "'  ";

            cmdtext3 = cmdtext3 + "    update yk_ckd1  set  je=dj*sl,cbje=cbdj*sl,ncsl=" + textBox7.Text.ToString() + ",bz='" +
                                textBox10.Text.ToString() + "',ypkc= " + textBox5.Text.ToString() + "     where ckdh=" +
                                 ckdh + "   and ypbm='" + textBox1.Text.ToString() + "'    ";
            DataRow dr = dt.NewRow();
            dr[0] = textBox1.Text.ToString();
            dr[1] = textBox2.Text.ToString();
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;


        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            select_bm();
        }

        public void select_bm()//下拉框双击选取药品
        {
            string cmdtext5 = "SELECT bm as 编码,MC AS 名称,GG as 规格  from zd_yp where  dh = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or bm = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
            SqlDataReader read2;
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext5, cn);
                SqlDataReader read1 = com.ExecuteReader();
                if (read1.Read())
                {
                    textBox1.Text = read1["编码"].ToString();
                    textBox2.Text = read1["名称"].ToString();
                    textBox3.Text = read1["规格"].ToString();

                }
                read1.Close();
            }
            //药品库存
            string cmdtext6 = "select SUM(SL) AS KCSL from YP_CRKJZ   where YPBM='" + textBox1.Text.ToString() + "'";

            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                SqlCommand com = new SqlCommand(cmdtext6, cn1);
                read2 = com.ExecuteReader();
            }

            if (read2.Read())
            {
                textBox5.Text = read2[0].ToString();
                if (textBox5.Text == "")
                    textBox5.Text = "0";
            }
            else
                textBox5.Text = "0";

            dataGridView2.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)//确定
        {
            cmdtext3 = cmdtext3 + "   COMMIT TRANSACTION ";
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn);
                    com.ExecuteNonQuery();
                }
            }
            catch (Exception ee)
            {
                cmdtext3 = "rollback";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn);
                    com.ExecuteNonQuery();
                }
                MessageBox.Show("ERROR!"+ee.ToString());
            }
            this.Close();
        }

        public void update_currentRow()//更改
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入药品");
                return;
            }
            cmdtext3 = cmdtext3 + "   DELETE from YK_CKD1 WHERE CKDH = " + ckdh + "  AND YPBM = '" + textBox1.Text.ToString() + "'  ";

            cmdtext3 = cmdtext3 + "    insert into yk_ckd1 (ckdh,ypbm,bzsl,autorkid,sl,dj,cbdj)  " +
                                  "    select top  " + rkd_count + "   " + ckdh + ",'" + textBox1.Text.ToString() + "',bzsl,autorkid,sysl,lsj,jj from yk_rkd1 where  ypbm='" +
                                    textBox1.Text.ToString() + "'  and sysl>0 order by autorkid    ";

            if (rkd_count > 1)
            {
                Decimal sysl1 = 0;
                for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
                    sysl1 = sysl1 + System.Convert.ToDecimal(dataGridView3[4, i].Value);
                cmdtext3 = cmdtext3 + "    update yk_ckd1 set sl=" + (System.Convert.ToDecimal(textBox6.Text) - sysl1) + "  where  autockid in (select top 1 autockid from yk_ckd1 where  ckdh=" +
                                  ckdh + "   and  ypbm='" + textBox1.Text.ToString() + "' order by autorkid desc)    ";
            }
            else
                cmdtext3 = cmdtext3 + "    update yk_ckd1  set sl=" + textBox6.Text.ToString() + 
                               "   from yk_ckd1   where ckdh=" +
                                ckdh + "   and ypbm='" + textBox1.Text.ToString() + "'    ";

            cmdtext3 = cmdtext3 + "  update   yk_ckd1 set dj=yp.bzdj  from yk_ckd1 left join zd_yp yp on ypbm=yp.bm   where ckdh=" + ckdh + "   and ypbm='" + textBox1.Text.ToString() + "'  ";
            cmdtext3 = cmdtext3 + "    update yk_ckd1  set  je=dj*sl,cbje=cbdj*sl,ncsl=" + textBox7.Text.ToString() + ",bz='" +
                                textBox10.Text.ToString() + "',ypkc= " + textBox5.Text.ToString() + "     where ckdh=" +
                                 ckdh + "   and ypbm='" + textBox1.Text.ToString() + "'    ";
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//药品编码press
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from zd_yp where dh like '" + textBox1.Text.Trim() + "%' or bm like '" + textBox1.Text.Trim() + "%' or mc like '" + textBox1.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView2.DataSource = dt;
                dataGridView2.Visible = true;
            }
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)//拟出数量
        {
            
            if (e.KeyChar == '\r')
            {
                dt3.Rows.Clear();
                dataGridView3.DataSource = dt3;
                if (System.Convert.ToDecimal(textBox7.Text) < System.Convert.ToDecimal(textBox5.Text))
                {

                    SqlConnection cn = new SqlConnection(SQL.conStr);
                    {
                        cn.Open();
                        string cmdtext4 = "select count(*) from yk_rkd1  where ypbm='" + textBox1.Text.ToString() + "'";
                        SqlCommand com1 = new SqlCommand(cmdtext4, cn);
                        int count = (int)com1.ExecuteScalar();
                        Decimal sysl = 0;
                        for (int i = 1; i <= count; i++)
                        {
                            cmdtext4 = "select sum(sysl) from yk_rkd1  where ypbm='" + textBox1.Text.ToString() + "' and rkdh in (select top " + i + " rkdh from yk_rkd1  where ypbm='" + textBox1.Text.ToString() + "' and sysl>0 order by autorkid)group by ypbm ";
                            SqlCommand com2 = new SqlCommand(cmdtext4, cn);
                            sysl = (Decimal)com2.ExecuteScalar();
                            if (sysl >= System.Convert.ToDecimal(textBox7.Text))
                            {
                                textBox6.Text = textBox7.Text;
                                string cmdtext7 = "select TOP  " + i + "  rkdh as 入库单号,pzwh as 批准文号,(select  convert(datetime,yxrq-2)) as 有效期,ph as 批号,sysl as 剩余数量,jj as 进价,lsj as 零售价,gys.mc as 供应商,bz as 备注" +
                                    "    from yk_rkd1  r1   LEFT JOIN YK_ZDGYS gys ON R1.gysbm = gys.BM    " +
                                    "where ypbm='" + textBox1.Text.ToString() + "' and sysl>0 order by autorkid";
                                SqlDataAdapter da = new SqlDataAdapter(cmdtext7, cn);
                                da.Fill(dt3);
                                dataGridView3.DataSource = dt3;
                                break;
                            }

                        }
                    }
                }
                else
                {
                    textBox6.Text = textBox5.Text;
                    SqlConnection cn = new SqlConnection(SQL.conStr);
                    {
                        cn.Open();
                        string cmdtext7 = "select   rkdh as 入库单号,pzwh as 批准文号,(select  convert(datetime,yxrq-2)) as 有效期,ph as 批号,sysl as 剩余数量,jj as 进价,lsj as 零售价,gys.mc as 供应商,bz as 备注" +
                                       "    from yk_rkd1  r1   LEFT JOIN YK_ZDGYS gys ON R1.gysbm = gys.BM    " +
                                       "where ypbm='" + textBox1.Text.ToString() + "' and sysl>0 order by autorkid";
                        SqlDataAdapter da = new SqlDataAdapter(cmdtext7, cn);
                        da.Fill(dt3);
                        dataGridView3.DataSource = dt3;
                    }
                }
                string cmdtext8 = "select bzdj from zd_yp where bm='" + textBox1.Text.ToString() + "'";
                SqlConnection cn1 = new SqlConnection(SQL.conStr);
                {
                    cn1.Open();
                    SqlCommand com = new SqlCommand(cmdtext8, cn1);
                    Decimal lsj = (Decimal)com.ExecuteScalar();
                    textBox11.Text = lsj.ToString();
                }
                textBox9.Text = dataGridView3[5, 0].Value.ToString();               
                textBox8.Text = "";
                rkd_count = dataGridView3.Rows.Count;
            }
        }



    }
}